package com.xhwl.centiotdata.suppliersystem.fushi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.constant.FuShiConstant;
import com.xhwl.centiotdata.pojo.dto.device.DeviceIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.dto.fushi.DeviceFuShiDTO;
import com.xhwl.centiotdata.pojo.dto.fushi.DeviceStatusFuShiDTO;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.enums.CategoryEnum;
import com.xhwl.common.enums.device.DeviceOnlineStatusEnum;
import com.xhwl.common.enums.device.DeviceTypeEnum;
import com.xhwl.common.pojo.cent.device.Device;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: yanyixiong
 * @Description: 富士-设备对接
 * @Date: create in 9:32 2023/4/23
 */
@Slf4j
@Service
public class DeviceFuShiHandler {

    @Autowired
    GeneralFuShiHandler generalFuShiHandler;

    /**
     * 拉取人行设备
     * @param configDTO
     * @return
     */
    public List<DeviceFuShiDTO> pullPedestrianDevice(DeviceIntegrationConfigDTO configDTO, String where){
        HashMap<String, Object> params = new HashMap<>();
        params.put("CurrentPage", 1);
        params.put("pageSize", 10000);
        params.put("OrderBy","ID");
        params.put("OrderType","true");
        params.put("where", where);
        JSONObject result = generalFuShiHandler.post(FuShiConstant.QUERY_PEDESTRIAN_DEVICE, params,
                configDTO.getIpAndPort(), configDTO.getTagId());
        JSONObject state = result.getJSONObject("State");
        if(state.getBoolean("IsSucess")){
            String recordStr = result.getString("Records");
            if (StringUtils.isEmpty(recordStr) || "null".equals(recordStr)) {
                log.error("post path:{};params:{};pageData is null!", FuShiConstant.QUERY_PEDESTRIAN_DEVICE, JSON.toJSONString(params));
                return new ArrayList<>();
            }
            List<DeviceFuShiDTO> deviceFuShiDTOList = JSONObject.parseArray(recordStr, DeviceFuShiDTO.class);
            return deviceFuShiDTOList;
        }else {
            log.error("项目{}供应商{}拉取门禁设备失败，原因是{}",configDTO.getProjectId(), configDTO.getProductId(), JSONObject.toJSONString(result));
            return new ArrayList<>();
        }
    }

    /**
     * 处理人行设备
     * @param configDTO
     * @param deviceFuShiDTOList
     * @return
     */
    public List<DeviceDTO> handlePedestrianDevice(DeviceIntegrationConfigDTO configDTO, List<DeviceFuShiDTO> deviceFuShiDTOList) {
        if(CollectionUtils.isEmpty(deviceFuShiDTOList)){
            return new ArrayList<>();
        }
        List<DeviceDTO> deviceDTOS = new ArrayList<>();
        deviceFuShiDTOList.forEach(deviceFuShiDTO -> {
            DeviceDTO deviceDTO = new DeviceDTO();
            deviceDTO.setThirdDeviceId(deviceFuShiDTO.getDevNo().toString());
            deviceDTO.setProductId(configDTO.getProductId());
            deviceDTO.setProjectId(configDTO.getProjectId());
            deviceDTO.setName(deviceFuShiDTO.getDevName());
            deviceDTO.setCategoryId(CategoryEnum.DOOR.getId());
            deviceDTO.setDeviceTypeId(DeviceTypeEnum.DOOR.getCode());
            deviceDTO.setSupplierId(7);
            deviceDTO.setIp(deviceFuShiDTO.getDevAddr());

            deviceDTOS.add(deviceDTO);
        });
        return deviceDTOS;
    }

    /**
     * 处理状态
     * @param configDTO
     * @param deviceStatusFuShiDTOList
     * @return
     */
    public List<Device> handlePedestrianDeviceOnlineStatus(DeviceIntegrationConfigDTO configDTO, List<DeviceStatusFuShiDTO> deviceStatusFuShiDTOList) {
        return deviceStatusFuShiDTOList.stream().map(deviceStatusFuShiDTO -> {
            Device device = new Device();
            device.setProjectId(configDTO.getProjectId());
            device.setThirdDeviceId(deviceStatusFuShiDTO.getDevNo().toString());
            device.setDeviceTypeId(configDTO.getDeviceType());
            device.setOnlineStatus(handleOnlineStatus(deviceStatusFuShiDTO) );
            return device;
        }).collect(Collectors.toList());
    }

    /**
     * 拉取人行设备的状态
     * @param configDTO
     * @return
     */
    public List<DeviceStatusFuShiDTO> pullPedestrianDeviceOnlineStatus(DeviceIntegrationConfigDTO configDTO) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("CurrentPage", 1);
        params.put("pageSize", 10000);
        JSONObject result = generalFuShiHandler.post(FuShiConstant.QUERY_PEDESTRIAN_DEVICE_STATUS, params,
                configDTO.getIpAndPort(), configDTO.getTagId());
        JSONObject state = result.getJSONObject("State");
        if(state.getBoolean("IsSucess")){
            String recordStr = result.getString("Records");
            if (StringUtils.isEmpty(recordStr)) {
                log.error("post path:{};params:{};pageData is null!", FuShiConstant.QUERY_PEDESTRIAN_DEVICE_STATUS, JSON.toJSONString(params));
                return new ArrayList<>();
            }
            List<DeviceStatusFuShiDTO> deviceStatusFuShiDTOList = JSONObject.parseArray(recordStr, DeviceStatusFuShiDTO.class);
            return deviceStatusFuShiDTOList;
        }else {
            log.error("项目{}供应商{}拉取门禁设备状态失败，原因是 {}",configDTO.getProjectId(), configDTO.getProductId(), JSONObject.toJSONString(result));
            return new ArrayList<>();
        }
    }

    private DeviceOnlineStatusEnum handleOnlineStatus(DeviceStatusFuShiDTO deviceStatusFuShiDTO){
        Integer doorState = deviceStatusFuShiDTO.getDoorState();
        if(doorState == null){
            return null;
        }
        switch (doorState){
            case -1:
                return DeviceOnlineStatusEnum.OFFLINE;
            case 0:
            case 1:
                return DeviceOnlineStatusEnum.ONLINE;
            default:
                return null;
        }
    }
}
